home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / stonehenge / Point.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  180 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. /*
  39.  * (c) Copyright 1993, Silicon Graphics, Inc.
  40.  * ALL RIGHTS RESERVED 
  41.  * Permission to use, copy, modify, and distribute this software for 
  42.  * any purpose and without fee is hereby granted, provided that the above
  43.  * copyright notice appear in all copies and that both the copyright notice
  44.  * and this permission notice appear in supporting documentation, and that 
  45.  * the name of Silicon Graphics, Inc. not be used in advertising
  46.  * or publicity pertaining to distribution of the software without specific,
  47.  * written prior permission. 
  48.  *
  49.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  50.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  51.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  52.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  53.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  54.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  55.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  56.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  57.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  58.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  59.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  60.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  61.  * 
  62.  * US Government Users Restricted Rights 
  63.  * Use, duplication, or disclosure by the Government is subject to
  64.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  65.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  66.  * clause at DFARS 252.227-7013 and/or in similar or successor
  67.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  68.  * Unpublished-- rights reserved under the copyright laws of the
  69.  * United States.  Contractor/manufacturer is Silicon Graphics,
  70.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  71.  *
  72.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  73.  */
  74. #include <GL/gl.h>
  75.  
  76. #include <math.h>
  77. #include <stdio.h>
  78.  
  79. #define POINT_EXTERN
  80. #include "Point.h"
  81.  
  82. Point Point::rotate_abouty(GLfloat c, GLfloat s)
  83. {
  84.   val.pt[0] = c*pt[0] + s*pt[2];
  85.   val.pt[1] = pt[1];
  86.   val.pt[2] = -s*pt[0] + c*pt[2];
  87.   return val;
  88. }
  89.  
  90. Point Point::rotate_aboutz(GLfloat c, GLfloat s)
  91. {
  92.   val.pt[0] = c*pt[0] - s*pt[1];
  93.   val.pt[1] = s*pt[0] + c*pt[1];
  94.   val.pt[2] = pt[2];
  95.   return val;
  96. }
  97.  
  98. void Point::refract_self(Point light, Point N, GLfloat I)
  99. {
  100.   GLfloat t;
  101.   Point dlight;
  102.  
  103.   dlight = refract_direction(light, N, I);
  104.   t = -pt[2] / dlight.pt[2]; 
  105.   pt[0] = pt[0] + dlight.pt[0]*t;
  106.   pt[1] = pt[1] + dlight.pt[1]*t;
  107.   pt[2] = 0;
  108. }
  109.  
  110. Point Point::refract_direction(Point light, Point N, GLfloat I) 
  111. {
  112.   GLfloat cos1, sin1, cos2, sin2, m;
  113.   GLfloat dlight[3], dN[3], axis[3];
  114.   
  115.   /* dlight = (light - *this).unit() * -1.0; */ 
  116.   dlight[0] = pt[0] - light.pt[0]; 
  117.   dlight[1] = pt[1] - light.pt[1]; 
  118.   dlight[2] = pt[2] - light.pt[2]; 
  119.   m = sqrt(dlight[0]*dlight[0] + dlight[1]*dlight[1] + dlight[2]*dlight[2]);
  120.   dlight[0] /= m;
  121.   dlight[1] /= m;
  122.   dlight[2] /= m;
  123.   
  124.   // dN = N * -1.0; 
  125.   dN[0] = -N.pt[0]; 
  126.   dN[1] = -N.pt[1]; 
  127.   dN[2] = -N.pt[2]; 
  128.   
  129.   // cos1 = dN.dot(dlight); 
  130.   cos1 = dN[0]*dlight[0] + dN[1]*dlight[1] + dN[2]*dlight[2];
  131.   
  132.   if (1.0 - cos1*cos1 < point_fudge) {
  133.     val = dN;
  134.     return val;
  135.   }
  136.   
  137.   // axis = ((dN * dlight) * dN).unit(); 
  138.   val.pt[0] = dN[1]*dlight[2] - dlight[1]*dN[2];
  139.   val.pt[1] = dN[2]*dlight[0] - dlight[2]*dN[0];
  140.   val.pt[2] = dN[0]*dlight[1] - dN[1]*dlight[0];
  141.   axis[0] = val.pt[1]*dN[2] - dN[1]*val.pt[2];
  142.   axis[1] = val.pt[2]*dN[0] - dN[2]*val.pt[0];
  143.   axis[2] = val.pt[0]*dN[1] - val.pt[1]*dN[0];
  144.   m = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
  145.   axis[0] /= m;
  146.   axis[1] /= m;
  147.   axis[2] /= m;
  148.   
  149.   if (axis[0]*axis[0] > point_fudge)  
  150.     sin1 = (dlight[0] - dN[0] * cos1) / axis[0]; 
  151.   else if (axis[1]*axis[1] > point_fudge)  
  152.     sin1 = (dlight[1] - dN[1] * cos1) / axis[1]; 
  153.   else sin1 = dlight[2] - dN[2] * cos1; 
  154.   
  155.   sin2 = sin1 / I; 
  156.   cos2 = (sin1*sin1 < 1.0) ? sqrt(1.0 - sin2*sin2) : 0; 
  157.   
  158.   dlight[0] = dN[0]*cos2 + axis[0]*sin2;
  159.   dlight[1] = dN[1]*cos2 + axis[1]*sin2;
  160.   dlight[2] = dN[2]*cos2 + axis[2]*sin2;
  161.   
  162.   /* I'm not sure this is quite legal */ 
  163.   if (dlight[2] > 0.0) dlight[2] = -dlight[2];   
  164.   
  165.   val = dlight;
  166.  
  167.   return val;
  168. }
  169.  
  170. void Point::print()
  171. {
  172.   print("%f %f %f\n");
  173. }
  174.  
  175. void Point::print(const char *format)
  176. {
  177.   printf(format, this->pt[0], this->pt[1], this->pt[2], 1.0);
  178. }            
  179.  
  180.